package org.dei.perla.sys.device.fpc;

import java.util.ArrayList;

import org.dei.perla.parser.clauses.OnUnsupportedSampleRateOptions;
import org.dei.perla.sys.llqruntime.datastructures.Record;
import org.dei.perla.utils.logger.LogRecord;
import org.dei.perla.utils.logger.LogRecord.Type;
import org.dei.perla.utils.logger.LogRecord.Verbosity;
import org.dei.perla.utils.messages.AdapterFpcMessage;
import org.dei.perla.utils.messages.MsgUtility;
import org.dei.perla.utils.pipe.Pipe;

public class SampleFPCTypeA extends FPC {

	public SampleFPCTypeA(String parText, AdapterFpcMessage parFrame, 
			Pipe<AdapterFpcMessage> parPipeIn, Pipe<AdapterFpcMessage> parPipeOut) {
		
		super(parText, parFrame, parPipeIn, parPipeOut);
		
	}

	@Override
	protected void mngNormalMsg(byte[] parPayload) {
		
		String tMessage = "Received the message <" + 
			new String(MsgUtility.getNormalMsgContent(parPayload)) + 
			"> from vci " + MsgUtility.getVciNormalMessage(parPayload) +"!";
		loggerNotify(Type.Information, tMessage, Verbosity.Medium);
		
		byte[] tFlag = MsgUtility.intToByteArray(MsgUtility.flagOthers);
		byte[] tVci = MsgUtility.intToByteArray(MsgUtility.getVciNormalMessage(parPayload));
		
		byte[] tMex = tMessage.getBytes();
		
		  byte [] tPayload = new byte [MsgUtility.flagLength + MsgUtility.channelIdLength + tMex.length];
	         
	         // Flag
	         for(int i = 0; i < MsgUtility.flagLength; i++) {
	        	 tPayload[i] = tFlag[i];     
	         }
	         
	         // Vci
	         for (int i = MsgUtility.flagLength; i < MsgUtility.flagLength + MsgUtility.channelIdLength; i++) {
	        	 tPayload[i] = tVci[i-4];
	         }
		        
	         // messaggio
	         for(int i = MsgUtility.flagLength + MsgUtility.channelIdLength; i < MsgUtility.flagLength + tVci.length + tMex.length; i++){
	        	 tPayload[i] = tMex[i-8];
	        	 
	         }
	         
		AdapterFpcMessage tFrame = new AdapterFpcMessage(null, tPayload);
		
		this.loggerNotify(LogRecord.Type.Information, "Sending reply to device...", LogRecord.Verbosity.Medium);
		this.pPipeOut.enqueue(tFrame);
	}

	@Override
	public boolean setSamplingTime(long parTime, OnUnsupportedSampleRateOptions parBehaviour) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Pipe<Record> registerQuery(ArrayList<String> parAttrbiutesNames) {
		// TODO Auto-generated method stub
		return null;
	}
}
